package com.corpgovernment.common.security.type;

import com.ctrip.corp.obt.generic.security.constants.SecurityRequest;
import com.ctrip.corp.obt.generic.security.constants.SecurityType;
import com.ctrip.corp.obt.generic.security.densification.service.desensitization.BaseDesensitizationSecurityKeyPairService;
import com.ctrip.corp.obt.generic.security.utils.DesensitizationUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;

import java.util.function.Function;

/**
 * 自定义:证件号脱敏类型
 *
 * @author xukang zhang
 * @date 2023/8/25 13:57
 */
public enum CardDesensitizationType implements SecurityType {
    /**
     * 身份证
     */
    ID_CARD("id_card", "身份证", (securityRequest -> DesensitizationUtils.desensitizeCenter(securityRequest.getContent(), 4, 3))),

    /**
     * 国内护照
     */
    PASSPORT("passport", "护照", securityRequest -> {
        String text = securityRequest.getContent();
        if (StringUtils.isBlank(text)) {
            return text;
        }
        return DesensitizationUtils.desensitizeCenter(text, text.length() / 3, 2);
    }),


    /**
     * 银行卡
     */
    BANK_CARD("bank_card", "银行卡", (securityRequest -> DesensitizationUtils.desensitizeCenter(securityRequest.getContent(), 6, 4))),

    /**
     * 国外护照
     */
    FOREIGN_PASSPORT("foreign_passport", "护照", securityRequest -> DesensitizationUtils.desensitizeCenter(securityRequest.getContent(), 3, 2)),

    /**
     * 未知
     */
    UNKNOWN("unknown", "未知", securityRequest -> DesensitizationUtils.desensitizeCenter(securityRequest.getContent(), 1, 2), BaseDesensitizationSecurityKeyPairService::decryptFunc);

    /**
     * 证件中文名称
     */
    private String name;

    /**
     * 证件类型字符串
     */
    private String cardType;


    /**
     * 脱敏逻辑
     */
    private Function<SecurityRequest, String> encryptFunc;

    /**
     * 脱敏后数据处理逻辑
     */
    private Function<SecurityRequest, String> decryptFunc;

    CardDesensitizationType(String cardType, String name, Function<SecurityRequest, String> encryptFunc) {
        this(cardType, name, encryptFunc, null);
    }

    CardDesensitizationType(String cardType, String name, Function<SecurityRequest, String> encryptFunc, Function<SecurityRequest, String> decryptFunc) {
        this.cardType = cardType;
        this.name = name;
        this.encryptFunc = encryptFunc;
        this.decryptFunc = decryptFunc;
    }

    @Override
    public String getType() {
        return cardType;
    }

    public Function<SecurityRequest, String> getEncryptFunc() {
        return encryptFunc == null ? UNKNOWN.encryptFunc : encryptFunc;
    }

    @Override
    public Function<SecurityRequest, String> getDecryptFunc() {
        return decryptFunc == null ? UNKNOWN.decryptFunc : decryptFunc;
    }

}
